﻿function renderFormData(obj) {

    for (var item in obj) {

        var input = $('.layui-input-block *[cid=' + item + ']');
        for (var i = 0; i < input.length; i++) {

            if (input.eq(i).is('input')) {
                input.eq(i).val(obj[item])
                console.log(input.eq(i).attr('type'))
            } else {
                input.eq(i).html(obj[item])
            }

        }
    }
}

function getQueryJson() {
    var data = {
        and: [],
        or: []
    }

    //文本框
    var oList = $('.search input[qn]');
    for (var i = 0; i < oList.length; i++) {
        if (oList.eq(i).val()) {
            data.and.push({ paraName: oList.eq(i).attr('qn'), paraValue: oList.eq(i).val() })
        }

    }
    //下拉菜单，往后查询一个元素
    oList = $('.search select[qn]');
    for (var i = 0; i < oList.length; i++) {
        var laySel = oList.eq(i).next().find('.layui-this');
        if (laySel.length && laySel.attr('lay-value')) {
            data.and.push({ paraName: oList.eq(i).attr('qn'), paraValue: laySel.attr('lay-value') })
        }

    }
    console.log(data)
    return JSON.stringify(data);
}

function addData() {
    window.location.href = appUrl + '/data';
}

function goBack() {
    window.history.go(-1);
}

function saveData() {


}
function fixData(data) {
    for (var Key in data) {

        if (Key.indexOf('|') > 0) {
            var col = Key.split('|')[0];
            if (!data[col]) { data[col] = ""; }
            data[col] += Key.split('|')[1] + ";";
            delete data[Key];
        }


    }
    return data;
}

function renderEdit($, form) {
    $.ajax({
        url: appUrl + '/getData',
        data: {
            id: id
        },
        success: function (res) {
            var data = JSON.parse(res);
            for (var item in data) {
                var obj = $('[name=' + item + ']');
                if (obj.length) {
                    if (obj[0].tagName == "INPUT" && obj[0].type == "text" || obj[0].tagName == "TEXTAREA") {
                        obj.val(data[item]);
                    } else if (obj[0].tagName == "SELECT") {
                        $("#" + item + " option[value=" + data[item] + "]").attr("selected", "selected");
                    } else if (obj[0].type == "radio") {
                        for (var i = 0; i < obj.length; i++) {
                            if (data[item] && data[item] == obj[i].value) {
                                obj[i].checked = true;
                            }
                        }
                    }

                } else {
                    //如果找不到，尝试寻找其他类型的控件，如checkbox、radio
                    var obj = $('[name^="' + item + '|"]');
                    for (var i = 0; i < obj.length; i++) {
                        var v = obj.eq(i).attr('name').split('|')[1];
                        if (data[item] && data[item].indexOf(v + ';') >= 0) {
                            obj.eq(i).attr('checked', "checked")
                        }
                    }
                }
            }
            form.render();
        }

    });
}